package org.pentaho.di.ui.trans.steps.shentong.oss.client;

import java.nio.charset.StandardCharsets;

public class ObjectUtils {

	public static ResponseBody putObject(String host, String user, String password, String bucket, String filename, byte[] data) {
		try {
			RequestBody requestBody = new RequestBody("PUT", bucket);
			requestBody.addMeta("Content-Type", "application/octet-stream");
			requestBody.setData(data);
			requestBody.setObject(filename);
			OssClient client = new OssClient(host, user, password);
			return client.execute(requestBody);
		} catch (Exception e) {
			throw e;
		}
	}

	public static String generateDownloadURL(String host, String user, String password, String bucket, String filename) {
		try {
			RequestBody requestBody = new RequestBody("GET", bucket);
			String url = "/generDownloadPath?bucketName=" + bucket + "&objectName=" + filename;
			OssClient client = new OssClient(host, user, password);
			ResponseBody resp = client.execute(url, requestBody);
			int code = resp.getStatusCode();
			if (code != 200) {
				throw new RuntimeException(resp.getData().toString());
			} else {
				return new String(((ObjectResult) resp.getData()).getData(), StandardCharsets.UTF_8);
			}
		} catch (Exception e) {
			throw e;
		}
	}
}
